﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using BehaviorModel.Block;
using BehaviorModel.Block.Generator;
using BehaviorModel.Block.ServiceChannel;
using BehaviorModel.Block.Splitter;

namespace QSDesigner.Windows
{
    /// <summary>
    /// Interaction logic for PropertiesWindow.xaml
    /// </summary>
    public partial class PropertiesWindow : Window, IDesignerWindow
    {
        private ResourceDictionary _resourceDictionary = new ResourceDictionary();

        public DesignerItem Item { get; set; }
        
        public PropertiesWindow()
        {
            InitializeComponent();
            Loaded += (sender, args) => Init();
        }

        public void Init()
        {
            Title = Item.BlockName;
            LoadResourceDictionaries();

            switch (Item.Block.Type)
            {
                case BlockType.Generator:
                    ((IGenerator)Item.Block).PropertyChanged += (sender, args) => LoadResourceDictionaries();
                    break;
                case BlockType.ServiceChannel:
                    ((IServiceChannel)Item.Block).PropertyChanged += (sender, args) => LoadResourceDictionaries();
                    break;
            }
        }

        private void LoadResourceDictionaries()
        {
            switch (Item.Block.Type)
            {

                case BlockType.Generator:

                    _resourceDictionary = new ResourceDictionary
                    {
                        Source =
                            new Uri(
                            "/QSDesigner;component/Resources/Properties/Generators' Properties/GeneratorsProperties.xaml",
                            UriKind.RelativeOrAbsolute)
                    };
                  
                    UpdateProperties("GeneratorsProperties");

                    break;

                case BlockType.ServiceChannel:

                    _resourceDictionary = new ResourceDictionary
                    {
                        Source =
                            new Uri(
                            "/QSDesigner;component/Resources/Properties/Service Device Properties/ServiceDeviceProperties.xaml",
                            UriKind.RelativeOrAbsolute)
                    };


                    UpdateProperties("ServiceDeviceProperties");

                    break;

                case BlockType.Splitter: 
                    _resourceDictionary = new ResourceDictionary
                    {
                        Source = 
                        new Uri(
                            "/QSDesigner;component/Resources/Properties/Splitter's Properties/SplittersProperties.xaml",
                            UriKind.RelativeOrAbsolute)
                    };

                    UpdateProperties("SplittersProperties");
                    break;
            }

        }

        private void UpdateProperties(String propertiesName)
        {
            var contentControl = DesignerProperties; 

            if (contentControl != null)
                contentControl.Content = _resourceDictionary[propertiesName];

            UpdateProperties(contentControl);
        }

        private void UpdateProperties(ContentControl contentControl)
        {
            var currentBlock = Item.Block; // get our current block
                       
            var groupBox = contentControl.Content as GroupBox;

            if (groupBox != null)
            {
                
                groupBox.DataContext = currentBlock;

                var stackPanel = new StackPanel();

                foreach (var c in LogicalTreeHelper.GetChildren(groupBox))
                {
                    if (c is StackPanel)
                    {
                        stackPanel = c as StackPanel;
                        break;
                    }
                }
                var count = VisualTreeHelper.GetChildrenCount(stackPanel);
                for (int i = 0; i < count; i++)
                {
                    var childVisual = (Visual) VisualTreeHelper.GetChild(stackPanel, i);
                    var grid = childVisual as Grid;
                    if (grid != null)
                    {
                        switch (currentBlock.Type)
                        {
                            case BlockType.Generator:
                                grid.Visibility = grid.Name == ((IGenerator) currentBlock).GeneratorType.ToString()
                                                      ? Visibility.Visible
                                                      : Visibility.Collapsed;
                                break;
                            case BlockType.ServiceChannel:
                                var block = ((IServiceChannel) currentBlock);
                                grid.Visibility = grid.Name == block.ChannelType + "_" + block.GeneratorType
                                                      ? Visibility.Visible
                                                      : Visibility.Collapsed;
                                break;                           
                        }

                    }
                }
            }
        }

    }
}
